package com.itboye.ihomebank.login;

import java.util.Observable;
import java.util.Observer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
/**
 * 忘记密码1（输入手机号）
 *
 */
public class PasswordActivityOne extends BaseOtherActivity implements Observer{
	ImageView close_icon;//返回图片
	TextView add_shap_title_tv;//标题
	EditText pass_one_tel;//手机号
	TextView pass_one_ok;//下一步
	
	private String  phone;
	private UserPresenter userPresenter;
	
		
		


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close_icon://返回
			finish();
			break;
		case R.id.pass_one_ok://下一步
			phone=pass_one_tel.getText().toString();
//			if(TextUtils.isEmpty(phone)){
//				T.showShort(PasswordActivityOne.this, "手机号码不能为空");
//				return;
//			}
//			if(!isMobileNO(phone)){
//				T.showShort(PasswordActivityOne.this, "请输入正确的电话号码！");
//				return;
//			}
			getyzm();
			break;

		default:
			break;
		}
		
	}
	/** 判断是否为手机号 */
	public boolean isMobileNO(String mobiles) {
		Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
		Matcher m = p.matcher(mobiles);
		return m.matches();
	}
	/**
	 * 获取验证码
	 */
	private void getyzm() {
		userPresenter = new UserPresenter(this);
		userPresenter.send(pass_one_tel.getText().toString(), "2");
	}
	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity=handlerError(data);
		if(entity!=null){
			if(!entity.getCode().equals(0)){
				if(entity.getEventType()==UserPresenter.send_success){
					String bean=(String) entity.getData();
					if(bean!=null){
						ByAlert.alert("验证码发送成功");
						Intent intent=new Intent(PasswordActivityOne.this,PasswordActivityTwo.class);
						intent.putExtra("phone", pass_one_tel.getText().toString());
						intent.putExtra("yzm", bean);
						startActivity(intent);
						PasswordActivityOne.this.finish();
					}else{
						ByAlert.alert("失败");
					}
				}if(entity.getEventType() == UserPresenter.send_fail){
					ByAlert.alert("验证码发送失败");
				}
			}
		}
		
	}

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_password_one;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void onInit() {
		// TODO Auto-generated method stub
		add_shap_title_tv.setText("忘记密码");
	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub
		
	}
	
	
/*	private void getyzm() {//获取验证码
		MyJsonRequest<String> request = new MyJsonRequest.Builder<String>()
				.apiVer("100").typeKey("BY_Message_send_sms")
				.param("mobile", pass_one_tel.getText().toString())
				.param("code_type", "2")
				.requestListener(new XRequestListener<String>() {

					@Override
					public void onResponse(String response) {
						T.showLong(PasswordActivityOne.this, response);
						Intent intent=new Intent(PasswordActivityOne.this,PasswordActivityTwo.class);
						intent.putExtra("phone", phone);
						startActivity(intent);
						
					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						if (exception instanceof CodeErrorException) {
							T.showLong(PasswordActivityOne.this, msg);
						} else {
							T.showLong(PasswordActivityOne.this, exception.toString());
						}

					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}*/
}
